package com.hnkywl.sts.service.xtgl;

import com.hnkywl.sts.dao.xtgl.CzrzDao;
import com.hnkywl.sts.entity.base.Fwpz;
import com.hnkywl.sts.entity.sys.Yg;
import com.hnkywl.sts.entity.xtgl.Czrz;
import com.hnkywl.sts.entity.xtgl.Dlrz;
import com.hnkywl.sts.interceptor.AuthInterceptor;
import com.hnkywl.sts.listener.UserInfo;
import com.hnkywl.sts.service.base.FwpzService;
import com.hnkywl.sts.service.sys.YgService;
import com.hnkywl.sts.util.BaseUtils;
import com.hnkywl.sts.util.StringFormat;
import net.ruixin.orm.hibernate.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: Administrator
 * Date: 13-5-8
 * Time: 下午4:07
 * To change this template use File | Settings | File Templates.
 */
@Service
public class CzrzService {
    private FwpzService fwpzService;

    private CzrzDao czrzDao;

    //操作日志查询显示
    public String findCzrzList(Long yhId, String yhXm, Date czrq, int pageNo, int pageSize) {
        Page<Czrz> czrzPage = this.czrzDao.findCzrzList(yhId, yhXm, czrq, pageNo, pageSize);
        List<Czrz> czrzList = czrzPage.getResult() == null ? new ArrayList<Czrz>() : czrzPage.getResult();
        String czrzJson = BaseUtils.fetchCollectionToJson(czrzList, "id", "yh.id", "yh.xm", "yh.zgjg.qc", "czrq", "dlip", "dlmac", "cznr");
        return "{\"total\":" + czrzPage.getTotalCount() + ",\"rows\":[" + czrzJson + "]}";
    }

    @Transactional
    public Czrz findCzrzDetail(Long id) {
        return this.czrzDao.get(id);
    }

    @Autowired
    public void setCzrzDao(CzrzDao czrzDao) {
        this.czrzDao = czrzDao;
    }

    @Autowired
    public void setFwpzService(FwpzService fwpzService) {
        this.fwpzService = fwpzService;
    }

    @Transactional
    public void operateLog(HttpServletRequest request) {
        String[] servlet = request.getServletPath().split("/");
        String action = servlet[servlet.length - 1];
        Fwpz fwpz = fwpzService.findByAction(action);
        if (fwpz == null) return;

        Czrz czrz = new Czrz();
        czrz.setYh(getCurrentYg(request.getSession()));
        czrz.setDlip(request.getSession().getAttribute("remoteIP").toString());
        czrz.setDlmac(request.getSession().getAttribute("macAddress").toString());
        czrz.setCreateUser(czrz.getYh());
        czrz.setCznr(createCznr(request, fwpz));
        czrz.setCznrdetail(createXxCznr(request, fwpz));
        czrz.setCzrq(new Date());
        czrz.setFwpz(fwpz);
        this.czrzDao.save(czrz);
    }

    private String createCznr(HttpServletRequest request, Fwpz fwpz) {
        String template = "操作员工【{0}】使用了【{1}】页面，行为为【{2}】。";
        return StringFormat.format(template, getCurrentYgName(request.getSession()),
                fwpz.getName(), fwpz.getInfo());
    }

    private String createXxCznr(HttpServletRequest request, Fwpz fwpz) {
        String template = "操作员工【{0}】使用了【{1}】页面，行为为【{2}】，关联字段为【{3}】，关联信息为【{4}】";
        String linkField = fwpz.getLinkfield() == null ? "" : fwpz.getLinkfield();
        return StringFormat.format(template, getCurrentYgName(request.getSession()),
                fwpz.getName(), fwpz.getInfo(), linkField, getLinkValue(request, linkField));
    }

    private String getLinkValue(HttpServletRequest request, String linkField) {
        if (linkField == null || "".equals(linkField)) return "";
        return request.getParameter(linkField) == null ? "" : request.getParameter(linkField);
    }

    private Yg getCurrentYg(HttpSession session) {
        Yg yg = new Yg();
        yg.setId((Long) ((Map) session.getAttribute(AuthInterceptor.USER_SESSION_KEY)).get("id"));
        return yg;
    }

    private String getCurrentYgName(HttpSession session) {
        return (String) ((Map) session.getAttribute(AuthInterceptor.USER_SESSION_KEY)).get("xm");
    }

    @Transactional
    public void loginLog(Dlrz dlrz) {
        Czrz czrz = new Czrz();
        czrz.setYh(dlrz.getDlr());
        czrz.setDlip(dlrz.getDlip());
        czrz.setCreateUser(dlrz.getDlr());
        czrz.setCznr(createLoginCznr(dlrz.getDlr().getXm()));
        czrz.setCznrdetail(czrz.getCznr());
        czrz.setCzrq(new Date());
        this.czrzDao.save(czrz);
    }

    private String createLoginCznr(String xm) {
        String template = "操作员工【{0}】登录了系统！";
        return StringFormat.format(template, xm);
    }

    public void logoutLog(Dlrz dlrz) {
        Czrz czrz = new Czrz();
        czrz.setYh(dlrz.getDlr());
        czrz.setDlip(dlrz.getDlip());
        czrz.setCreateUser(dlrz.getDlr());
        czrz.setCznr(createLogoutCznr(dlrz.getDlr().getXm()));
        czrz.setCznrdetail(czrz.getCznr());
        czrz.setCzrq(new Date());
        this.czrzDao.save(czrz);
    }

    private String createLogoutCznr(String xm) {
        String template = "操作员工【{0}】退出了系统！";
        return StringFormat.format(template, xm);
    }
}
